package com.eat.fish_service.netty_client.utils;

/**
 * @author CZF
 * @create 2022-04-13 19:15
 * <p>
 * 把字节数组转换为int类型
 */

public class BytesToInt {

    public static int fourBytesToInt(byte[] bytes) {
        if (bytes.length != 4) {
            throw new RuntimeException("数组长度不等于4！");
        }
        int result = 0;
        int a = (bytes[0] & 0xff) << 24;
        int b = (bytes[1] & 0xff) << 16;
        int c = (bytes[2] & 0xff) << 8;
        int d = (bytes[3] & 0xff);
        result = a | b | c | d;
        System.out.println("转换后结果：" + result);
        return result;
    }

    public static int oneByteToInt(byte[] bytes) {
        return bytes[0] & 0xff;
    }

    public static int oneByteToInt(byte b) {
        return b & 0xff;
    }
}
